//
//  StatusToolBar.m
//  CC
//
//  Created by Calvin on 5/19/15.
//  Copyright (c) 2015 Calvin. All rights reserved.
//

#import "StatusToolBar.h"
#import "UIImage+Extension.h"
#import "StatusModel.h"

@interface StatusToolBar()
@property(nonatomic,strong)NSMutableArray *dividers;
@property(nonatomic,strong)NSMutableArray *btns;
@property(nonatomic,strong)UIButton *commentBtn;
@property(nonatomic,strong)UIButton *repostBtn;
@property(nonatomic,strong)UIButton *attitudesBtn;
@end

@implementation StatusToolBar
-(NSMutableArray *)dividers
{
    if (_dividers==nil) {
        _dividers =[NSMutableArray array];
    }
    
    return _dividers;
}

-(NSMutableArray *)btns
{
    if (_btns==nil) {
        _btns = [NSMutableArray array];
    }
    
    return _btns;
}

- (instancetype)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self) {
        self.userInteractionEnabled = YES;
        self.image=[UIImage resizedImage:@"timeline_card_bottom_background"];
        self.commentBtn = [self setupBtnWithIcon:@"timeline_icon_comment" title:@"评论"];
        self.repostBtn =  [self setupBtnWithIcon:@"timeline_icon_retweet" title:@"转发"];
        self.attitudesBtn = [self setupBtnWithIcon:@"timeline_icon_unlike" title:@"赞"];
        [self setupDividers];
        [self setupDividers];
    }
    return self;
}

-(UIButton *)setupBtnWithIcon:(NSString *)icon title:(NSString *)title
{
    UIButton *btn = [[UIButton alloc] init];
    [btn setImage:[UIImage imageWithName:icon] forState:UIControlStateNormal];
    [btn setTitle:title forState:UIControlStateNormal];
    [btn setTitleColor:[UIColor lightGrayColor] forState:UIControlStateNormal];
    btn.titleLabel.font = [UIFont systemFontOfSize:14];
    btn.titleEdgeInsets=UIEdgeInsetsMake(0, 15, 0, 0);
    [btn setBackgroundImage:[UIImage imageWithName:@"common_card_bottom_background_highlighted"] forState:UIControlStateHighlighted];
    btn.adjustsImageWhenHighlighted=NO;
    [self addSubview:btn];
    [self.btns addObject:btn];
    return btn;
}

-(void)setupDividers
{
    UIImageView *dividers=[[UIImageView alloc]init];
    dividers.image=[UIImage imageWithName:@"timeline_card_bottom_line"];
    dividers.contentMode=UIViewContentModeCenter;
    [self addSubview:dividers];
    [self.dividers addObject:dividers];
}

-(void)layoutSubviews
{
    [super layoutSubviews];
    NSInteger btnCount=self.btns.count;
    CGFloat btnW=self.frame.size.width/btnCount;
    CGFloat btnH=self.frame.size.height;
    for (int i =0 ; i<btnCount; i++) {
        UIButton *btn=self.btns[i];
        CGRect frame = CGRectMake(i*btnW, 0, btnW, btnH);
        btn.frame=frame;
    }
    
    NSInteger dividersCount=self.dividers.count;
    for (int i =0; i<dividersCount; i++) {
        UIImageView *divider=self.dividers[i];
        CGRect frame = CGRectMake((1+i)*btnW,btnH*0.5, 2, btnH);
        divider.frame=frame;
    }
}

-(void)setStatus:(StatusModel *)status
{
    _status =status;
    NSString *commenttitle=status.comments_count?[NSString stringWithFormat:@"%ld",(NSInteger)status.comments_count]:@"评论";
    [self.commentBtn setTitle:commenttitle forState:UIControlStateNormal];
    
    NSString *reposttitle=status.reposts_count?[NSString stringWithFormat:@"%ld",(NSInteger)status.reposts_count]:@"转发";
    [self.repostBtn setTitle:reposttitle forState:UIControlStateNormal];
    
    NSString *attitudestitle=status.attitudes_count?[NSString stringWithFormat:@"%ld",(NSInteger)status.attitudes_count]:@"赞";
    [self.attitudesBtn setTitle:attitudestitle forState:UIControlStateNormal];
}
@end
